package com.app.yc.ifapp.adapter;

/*
 * 作者：TanTun
 * 时间：2017/2/19
 * 邮箱：32965926@qq.com
 * 描述：ListView适配器
 */

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.List;

public class MyBaseAdapter<TObject> extends BaseAdapter {

    private static final String TAG = "MyBaseAdapter";
    private List<TObject> mContentList;
    private LayoutInflater mInflater;
    private int mViewId;
    private Callback<TObject> mCallback;

    public interface Callback<T> {
        void onGetView(int i, View v, T obj);
    }

    public MyBaseAdapter(Context context,
                         int id,
                         List<TObject> data,
                         Callback<TObject> callback) {

        mContentList = data;
        mInflater = LayoutInflater.from(context);
        mViewId = id;
        mCallback = callback;
    }

    @Override
    public int getCount() {
        return mContentList.size();
    }

    @Override
    public Object getItem(int i) {
        return mContentList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup parent) {
        try {
            if (view == null) view = mInflater.inflate(mViewId, null);
            mCallback.onGetView(i, view, mContentList.get(i));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return view;
    }
}